﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class ListAlbumWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.ListAlbumWP";
            }
        }


        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("FTitle", "CDSP-Danh sách Album ảnh");
            }
        }



        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("DefaultView",GetText("DefaultView","Ngầm định (Chuẩn)"),1)
                };
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "ListAlbumWP";
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string belongSite)
        {
            StringBuilder RetHtml = new StringBuilder();
            try
            {
                DataTable dtItems = new FAlbumBLL().ReadingAllAlbum(belongSite);

                RetHtml.Append(
                        "<div class=\"PageTitleBar\">\n" +
                        "				<ul class=\"breadcrumb\">\n" +
                        "					<li class=\"breadcrumblv1\"><a href=\"#\">Thư viện ảnh</a></li>\n" +
                        "				</ul>\n" +
                        "				<div class=\"clr\"></div>\n" +
                       "</div>" +
                        "<div class=\"ArticleDetail\">\n" +
                        "<ul class=\"listnews\">");
                if (dtItems.Rows.Count == 0)
                {
                    RetHtml.Append(
                        "<div class=\"clrfix\"></div>" +
                        "<div class=cssAccessDenided>" + ResourceBLL.GetText(WebPartId, "NoAlbum", "Hiện tại không có album nào!") + "</div>");
                }
                else
                {
                    string AlbumType = (string)dtItems.Rows[0]["AlbumType"];

                    for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
                    {
                        string AlbumId = (string)dtItems.Rows[iIndex]["AlbumId"];                        
                        string Subject = "";
                        try
                        {
                            Subject = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "Subject"];
                        }
                        catch { Subject = string.Empty; }
                        //string Description = "";
                        //try
                        //{
                        //    Description = (string)dtItems.Rows[iIndex][Environments.ActiveLanguage + "Description"];
                        //}
                        //catch { Description = string.Empty; }

                        //DataTable dtSub = new FAlbumBLL().ReadingItems(AdvImageId);
                        //if (dtSub.Rows.Count > 0)
                        //{
                            //string AdvImageItemId = (string)dtSub.Rows[0]["AdvImageItemId"];
                        string FileImage = (string)dtItems.Rows[0]["AvatarFileName"];
                        string ImageUrl  = "";
                        if (!string.IsNullOrEmpty(FileImage))
                        {
                            ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/Album/Images/" + belongSite + "/" + AlbumId + "/" + "avatar/" + FileImage;
                        }
                        else
                        {
                            ImageUrl = "";
                        }
                        string Url = Ultility.GetAlbumDetails(belongSite, Subject, AlbumId.Substring(0, 8), AlbumType);

                        int NumImg = new FAlbumBLL().GetNumImgByAlbumId(AlbumId);

                            RetHtml.Append(
                                     "<li>" +
                                            "<a href=\"" + Url + "\">" +
                                                "<img src=\"" + ImageUrl + "\" alt=\"" + Subject + "\" class=\"albumcover\" />" +
                                                "<span class=\"newstitle\">" + Subject + "</span>" +
                                            "</a>" +
                                            "<span class=\"greynews\">" + GetText("CountImagesInAlbum", "Số lượng ảnh: ") + NumImg + "</span>" +
                                            "<p>" + Description + "</p>" +
                                            "<div class=colfix></div>" +
                                    "</li>");
                       // }
                            FileImage = "";
                    }                    
                }
                RetHtml.Append(
                            "</ul>" +
                        "</div>");
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
            return RetHtml.ToString();
        }

        private string ClientRenderWebPart(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string belongSite = TabModuleCls.belongSite;
            try
            {
                StringBuilder RetHtml = new StringBuilder();               
                RetHtml.Append(
                    "<div id=\"leftsub\">" + GetHtml(belongSite) +
                    "</div>");
                return RetHtml.ToString();
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("DefaultView"))
                {
                    return ClientRenderWebPart(TabModuleId, HasPorlet);
                }
                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override DataTable ReadingDataService(string belongSite,
          string ActiveLanguage,
          ref string IdField,
          ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "3E681356-1234-413a-832B-F9B9EA1960BA","Danh sách album ảnh"
            });
            return dtResult;
        }

        //public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        //{
        //    IdField = "Id";
        //    SubjectField = "Subject";
        //    string Query = "select Id=AlbumId,Subject=[" + ActiveLanguage + "Subject] from TableAlbum where belongSite='" + belongSite + "'";
        //    SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
        //    sqlConnection.Open();
        //    SqlCommand sqlCommand = sqlConnection.CreateCommand();
        //    sqlCommand.CommandText = Query;
        //    DataTable dtResult = new DataTable();
        //    SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
        //    da.Fill(dtResult);
        //    return dtResult;
        //}

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }
    }
}
